package com.lzs.kunei.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.entity.*;
import com.lzs.kucun.service.StockService;
import com.lzs.kunei.service.InventoryLocatorDetailService;
import com.lzs.kunei.service.InventoryLocatorService;
import com.lzs.kunei.service.InventoryService;
import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpy
 * @since 2020-11-24
 */
@ApiModel(value = "盘点货位明细")
@RestController
@RequestMapping("/kunei/inventorylocatordetail")
public class InventoryLocatorDetailController {
    @Autowired
    private InventoryLocatorDetailService inventoryLocatorDetailService;
    @Autowired
    private StockService stockService;
    @Autowired
    private InventoryLocatorService inventoryLocatorService;
    @Autowired
    private InventoryService inventoryService;
    @ApiOperation(value = "添加该货位的盘点明细")
    @PostMapping("/addCommodity")
    public R addCommodity(@RequestBody InventoryLocatorDetail inventoryLocatorDetail, HttpServletRequest request){
        try {
            String token=request.getHeader("token");
            Long userid= JwtUtils.getMemberIdByJwtToken(token);
            User user=stockService.selectUser(userid);
            String name = user.getName();
            Integer qty = inventoryLocatorDetail.getQty();
            String inventoryno = inventoryLocatorDetail.getInventoryno();
            String commoditybar = inventoryLocatorDetail.getCommoditybar();
            String locatorcode = inventoryLocatorDetail.getLocatorcode();
            Long warehouseid = inventoryLocatorDetail.getWarehouseid();
            Locator locator = stockService.selectLocator(locatorcode);
            Commodity commodity = stockService.selectCommodity(commoditybar);
            inventoryLocatorDetail.setLocatorid(locator.getId());
            inventoryLocatorDetail.setLocatorname(locator.getName());
            inventoryLocatorDetail.setCommodityid(commodity.getId());
            inventoryLocatorDetail.setCommodityname(commodity.getName());
            inventoryLocatorDetail.setCreateid(userid);
            inventoryLocatorDetail.setCreator(name);
            QueryWrapper<InventoryLocatorDetail> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("locatorcode",locatorcode);
            queryWrapper.eq("inventoryno",inventoryno);
            queryWrapper.eq("commoditybar",commoditybar);
            //Integer cqty = stockService.selectQuantity(warehouseid, commoditybar, locatorcode).getCqty();
            Boolean aBoolean = inventoryLocatorDetailService.selectIsExist(locatorcode, inventoryno, commoditybar);
            InventoryLocatorDetail detail=new InventoryLocatorDetail();
            //System.out.println(cqty);
//            if (cqty > qty) {
//                inventoryLocatorDetail.setResult("1");
//                inventoryLocatorDetailService.save(inventoryLocatorDetail);
//            }
//            if (cqty < qty) {
//                inventoryLocatorDetail.setResult("2");
//                inventoryLocatorDetailService.save(inventoryLocatorDetail);
//            }
            if (!aBoolean){
                inventoryLocatorDetail.setResult("0");
            inventoryLocatorDetailService.save(inventoryLocatorDetail);
            }else {
                queryWrapper.eq("result","0");
                Integer qty1 = inventoryLocatorDetailService.inventoryGetQty(locatorcode, inventoryno, commoditybar, "0");
                detail.setQty(qty+qty1);
                inventoryLocatorDetailService.update(detail,queryWrapper);
            }

            return R.ok().message("添加盘点商品成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("添加盘点商品失败");
        }
    }
    @ApiOperation(value = "录入商品，点结束时")
    @PostMapping("/endState")
    public R endState(@RequestBody InventoryLocator inventoryLocator){
        try {
            String inventoryno = inventoryLocator.getInventoryno();
            String locatorcode = inventoryLocator.getLocatorcode();
            QueryWrapper<InventoryLocatorDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("inventoryno", inventoryno);
            queryWrapper1.eq("locatorcode", locatorcode);
            List<InventoryLocatorDetail> list = inventoryLocatorDetailService.list(queryWrapper1);
            if (list==null||list.size()==0){
                return R.error().message("未录入商品,请录入后结束");
            }
            for (int i = 0; i <list.size() ; i++) {
                InventoryLocatorDetail detail = list.get(i);
                String inventoryno1 = detail.getInventoryno();
                String commoditybar1 = detail.getCommoditybar();
                String locatorcode1 = detail.getLocatorcode();
                Long warehouseid1 = detail.getWarehouseid();
                Integer cqty = stockService.selectQuantity(warehouseid1, commoditybar1, locatorcode1).getCqty();
                Integer qty = inventoryLocatorDetailService.inventoryGetQty(locatorcode1, inventoryno1, commoditybar1, "0");
                if (qty>cqty){
                    detail.setQty(qty-cqty);
                    detail.setResult("2");
                    inventoryLocatorDetailService.save(detail);
                }
                if (qty<cqty){
                    detail.setQty(cqty-qty);
                    detail.setResult("1");
                    inventoryLocatorDetailService.save(detail);
                }
            }
            QueryWrapper<InventoryLocator> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("inventoryno", inventoryno);
            queryWrapper.eq("locatorcode", locatorcode);
            InventoryLocator inventoryLocator1 = new InventoryLocator();
            inventoryLocator1.setState("2");

            inventoryLocatorService.update(inventoryLocator1, queryWrapper);
            return R.ok().message("录入结束");
        }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("录入结束失败");
        }

    }

    @ApiOperation(value = "该货位的商品下拉")
    @PostMapping("/selectCommodity")
    public R selectCommoditybar(@RequestBody InventoryLocatorDetail inventoryLocatorDetail){
        String inventoryno = inventoryLocatorDetail.getInventoryno();
        String locatorcode = inventoryLocatorDetail.getLocatorcode();
        Long warehouseid = inventoryService.selectWarehouseid(inventoryno);
        return stockService.selectCommoditybar(locatorcode, warehouseid);
    }

    @ApiOperation(value = "查询盘点的结果")
    @PostMapping("/selectResult")
    public R selectCommodity0(@RequestBody InventoryLocator inventoryLocator){
        try {
            String inventoryno = inventoryLocator.getInventoryno();
            String locatorcode = inventoryLocator.getLocatorcode();
            List<InventoryLocatorDetail> inventoryLocatorDetails0 = inventoryLocatorDetailService.selectResult(locatorcode, inventoryno, "0");
            List<InventoryLocatorDetail> inventoryLocatorDetails1 = inventoryLocatorDetailService.selectResult(locatorcode, inventoryno, "1");
            List<InventoryLocatorDetail> inventoryLocatorDetails2 = inventoryLocatorDetailService.selectResult(locatorcode, inventoryno, "2");
            return R.ok().message("查询盘点结果成功")
                    .data("inventoryLocatorDetails0",inventoryLocatorDetails0)
                    .data("inventoryLocatorDetails1",inventoryLocatorDetails1)
                    .data("inventoryLocatorDetails2",inventoryLocatorDetails2);
        }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("查看盘点结果失败");
        }

    }

}

